home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 07511000 / var0898.dms / var0898.adf / ScsiTape / source.lha / util / rewind.c < prev    next >
C/C++ Source or Header  |  1991-11-15  |  3KB  |  157 lines

  1.  
  2. /*
  3.  *  REWIND
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/ports.h>
  8. #include <exec/io.h>
  9. #include <exec/memory.h>
  10. #include <devices/scsidisk.h>
  11. #include <libraries/dos.h>
  12. #include <libraries/dosextens.h>
  13. #include <libraries/filehandler.h>
  14. #include <clib/exec_protos.h>
  15. #include <clib/alib_protos.h>
  16.  
  17. typedef unsigned char ubyte;
  18.  
  19. typedef struct MsgPort    MsgPort;
  20. typedef struct IOStdReq IOStdReq;
  21. typedef struct SCSICmd    SCSICmd;
  22.  
  23. typedef struct SCSIReq {
  24.     IOStdReq sr_Req;
  25.     SCSICmd  sr_Cmd;
  26. } SCSIReq;
  27.  
  28. MsgPort *IoSink;
  29. SCSIReq Ios;
  30.  
  31. char *ErrorAry[] = {
  32.     "No Sense",
  33.     "Recovered Error",
  34.     "Not Ready",
  35.     "Medium Error",
  36.     "Hardware Error",
  37.     "Illegal Request",
  38.     "Unit Attention",
  39.     "Data Protect",
  40.     "Blank Check",
  41.     "Vendor Specific",
  42.     "Copy Aborted",
  43.     "Aborted Command",
  44.     "Equal (search_data)",
  45.     "Volume Overflow",
  46.     "MisCompare",
  47.     "Reserved",
  48.     "Unknown Error"
  49. };
  50.  
  51. void myexit(void);
  52.  
  53. main(ac, av)
  54. char *av[];
  55. {
  56.     char rewindCmd[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
  57.     int error;
  58.  
  59.     atexit(myexit);
  60.  
  61.     IoSink = CreatePort(NULL, 0);
  62.  
  63.     if (SCSIOpen("scsi.device", 4) < 0) {
  64.     puts("Unable to open scsi.device");
  65.     exit(20);
  66.     }
  67.     if (error = DoSCSI(rewindCmd, NULL, 0, SCSIF_READ, NULL))
  68.     printf("error %d %s\n", error, ErrorAry[(error >= 0 && error < 16) ? error : 16]);
  69.     return(0);
  70. }
  71.  
  72. void
  73. myexit(void)
  74. {
  75.     SCSIClose();
  76.     if (IoSink)
  77.     DeletePort(IoSink);
  78. }
  79.  
  80.  
  81. int
  82. SCSIOpen(devName, unitNo)
  83. char *devName;
  84. long unitNo;
  85. {
  86.     int error;
  87.  
  88.     Ios.sr_Req.io_Message.mn_ReplyPort = IoSink;
  89.     if (error = OpenDevice(devName, unitNo, &Ios.sr_Req, 0))
  90.     error = -1;
  91.     return(error);
  92. }
  93.  
  94. int
  95. SCSIClose()
  96. {
  97.     if (Ios.sr_Req.io_Device) {
  98.     CloseDevice(&Ios);
  99.     Ios.sr_Req.io_Device = NULL;
  100.     }
  101. }
  102.  
  103. int
  104. DoSCSI(cmd, buf, bytes, flags, len)
  105. char *cmd;
  106. char *buf;
  107. long bytes;
  108. ubyte flags;
  109. long *len;
  110. {
  111.     static ubyte senseCmd[] = { 0x03, 0x00, 0x00, 0x00, 20, 0 };
  112.     SCSIReq *ios = &Ios;
  113.     char senseBuf[20];
  114.     int error;
  115.  
  116.     ios->sr_Cmd.scsi_Command   = cmd;
  117.     ios->sr_Cmd.scsi_CmdLength = 6;
  118.     ios->sr_Cmd.scsi_Data   = buf;
  119.     ios->sr_Cmd.scsi_Length = bytes;
  120.     ios->sr_Cmd.scsi_Flags  = flags;
  121.  
  122.     ios->sr_Req.io_Command = 28;
  123.     ios->sr_Req.io_Data    = &ios->sr_Cmd;
  124.     ios->sr_Req.io_Length  = sizeof(ios->sr_Cmd);
  125.  
  126.     error = DoIO(&ios->sr_Req);
  127.     if (len)
  128.     *len = ios->sr_Cmd.scsi_Actual;
  129.  
  130.     switch(error) {
  131.     case 0:
  132.     break;
  133.     case HFERR_BadStatus:
  134.     ios->sr_Cmd.scsi_Command   = senseCmd;
  135.     ios->sr_Cmd.scsi_CmdLength = 6;
  136.     ios->sr_Cmd.scsi_Data    = (UWORD *)senseBuf;
  137.     ios->sr_Cmd.scsi_Length = sizeof(senseBuf);
  138.     ios->sr_Cmd.scsi_Flags    = SCSIF_READ;
  139.  
  140.     if (DoIO(&ios->sr_Req)) {
  141.         error = -256;
  142.         break;
  143.     }
  144.     if (ios->sr_Cmd.scsi_Actual < 4) {
  145.         error = -257;
  146.         break;
  147.     }
  148.     error = senseBuf[2];
  149.     break;
  150.     default:
  151.     error = -error;
  152.     break;
  153.     }
  154.     return(error);
  155. }
  156.  
  157.